<meta charset="UTF-8">
<?php
require ('./lib/init.php');

//查询5个栏目
$sql = "select cat_num,cat_name,cat_id from cat order by cat_id limit 5";
$cats = mGetAll($sql);

//查询评论数最高的5篇文章
$sql = "select title,art_id from art order by comm desc limit 5";
$hotarts = mGetAll($sql);

//查询最新四条评论评论
$sql = "select content from comment order by comment_id desc limit 5";
$comments = mGetAll($sql);

//判断地址栏是否有cat_id
 if (isset($_GET['cat_id'])){
     $where = " and art.cat_id = $_GET[cat_id]" ;
 }else{
     $where = "";
 }

//分页代码
$sql = "select count(*) from art where 1 " . $where;//总文章数量
$num = mGetOne($sql);//总文章
$curr = isset($_GET['page']) ? $_GET['page'] : 1;//当前页码
$cnt = 4 ;//每页显示条数
$page = getPage($num,$curr,$cnt);

 //查询所有的文章
$sql = "select art.art_id,author,title,content,pubtime,comm,cat_name,thumb from art inner join cat on 
art.cat_id = cat.cat_id where 1 " . $where . ' order by art_id desc limit ' . ($curr-1) * $cnt . ',' . $cnt;
$arts = mGetAll($sql);

// 当前栏目无文章 跳转到首页
if (empty($arts)){
    header('location:index.php');
}
require(ROOT . '/view/front/index.html');

?>
